import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UploadedFile,
  UseInterceptors,
  UploadedFiles,
  HttpException,
  HttpStatus,
  Res,
  Query,
  Logger,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiProperty,
  ApiConsumes,
} from '@nestjs/swagger';
import {
  FileInterceptor,
  MulterModule,
  FilesInterceptor,
} from '@nestjs/platform-express';
import { Response } from 'express';

import { ComMinioService } from './com-minio.service';
import { JwtNoVerify } from '../../decorators';

@Controller('com-minio')
export class ComMinioController {
  constructor(private readonly _comMinioService: ComMinioService) {}

  // 参考文档
  // https://juejin.cn/post/7477412987210514432

  @Post('upload-single-file')
  @UseInterceptors(FileInterceptor('file'))
  @ApiOperation({ summary: '上传单个文件' })
  async uploadFile(@UploadedFile() file: Express.Multer.File) {
    return await this._comMinioService.upload(file);
  }

  @Post('upload-files')
  @UseInterceptors(FilesInterceptor('files', 10)) // 允许上传最多10个文件
  @ApiOperation({ summary: '上传多个文件' })
  async uploadFiles(@UploadedFiles() files: Express.Multer.File[]) {
    return await this._comMinioService.uploadMultiple(files);
  }

  @Get('delete')
  @ApiOperation({
    summary: '根据文件名称+后缀删除文件，例如《二次续签附件.pdf》',
  })
  async deleteFile(@Query('fileName') fileName: string) {
    return await this._comMinioService.deleteFile(fileName);
  }

  @Get('download')
  @JwtNoVerify()
  @ApiOperation({ summary: '下载文件 - 有问题 暂时不可用' })
  async downloadFile(
    @Query('filename') filename: string,
    @Res() res: Response,
  ) {
    await this._comMinioService.getFileStream(res, filename);
  }

  @Get('get-bucket-list')
  @ApiOperation({ summary: '获取某个存储桶的所有内容' })
  async getBucketList(bucketName: string) {
    return await this._comMinioService.getDirectoryStructure(bucketName);
  }
}
